package com.uusee.crawler.job.baike;

import java.io.InputStream;
import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.uusee.crawler.dbwriter.BaikeDataSourceDbWriter;
import com.uusee.crawler.dbwriter.BaikeDbWriter;
import com.uusee.crawler.fetcher.FetchHTTP;
import com.uusee.crawler.framework.AbstractJob;
import com.uusee.crawler.model.CrawlStatusCodes;
import com.uusee.crawler.model.CrawlURI;
import com.uusee.crawler.pageprocessor.baike.BaikeDataSourcePageProcessor;
import com.uusee.crawler.pageprocessor.baike.BaikePageProcessor;
import com.uusee.crawler.pageprocessor.baike.mtime.MoviePlotsPageProcessor;
import com.uusee.crawler.pageprocessor.baike.mtime.RatingInterfacePageProcessor;
import com.uusee.crawler.xmlwriter.BaikeXmlWriter;
import com.uusee.framework.bo.UniversalBo;
import com.uusee.shipshape.bk.model.Baike;
import com.uusee.shipshape.bk.model.BaikeDataSource;

public class MtimeBaikeFastCrawlJob extends AbstractJob {
	FetchHTTP fetch = new FetchHTTP();
	BaikeDataSourcePageProcessor overviewPageProcessor;
	MoviePlotsPageProcessor plotsPageProcessor = new MoviePlotsPageProcessor();
	RatingInterfacePageProcessor ratingPageProcessor = new RatingInterfacePageProcessor();
	private BaikeDataSourceDbWriter dbWriter = new BaikeDataSourceDbWriter();

	public CrawlURI createCrawlURI(String id) {
		CrawlURI crawlURI = new CrawlURI();
		String crawlUrl = "http://www.mtime.com/movie/" + id + "/";
		crawlURI.setSourceSite("www.mtime.com");
		crawlURI.setCrawlUrl(crawlUrl);
		return crawlURI;
	}

	public void crawl(CrawlURI crawlURI) {
		try {
			// 抓取概览数据
			fetch.process(crawlURI);
			overviewPageProcessor.process(crawlURI);
			if (crawlURI.getCrawlStatus() == CrawlStatusCodes.PAGE_PROCESS_SUCCESS) {
				BaikeDataSource bds = (BaikeDataSource)crawlURI.getModel();
				String oriId = bds.getOriId();
				// 抓取评分数据
				String crawlUrl = "http://www.mtime.com/database/databaseService.m?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Community.Controls.CommunityPages.DatabaseService&Ajax_CallBackMethod=LoadData&Ajax_RequestUrl=http%3A%2F%2Fwww.mtime.com%2Fmovie%2F"
							+ oriId + "%2F&Ajax_CallBackArgument0=1&Ajax_CallBackArgument1=" + oriId;
				crawlURI.setCrawlUrl(crawlUrl);
				crawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);
				fetch.process(crawlURI);
				ratingPageProcessor.process(crawlURI);

				// 抓取剧情
				crawlUrl = "http://www.mtime.com/database/ShowPlotService.m?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Community.Controls.CommunityPages.ShowPlotService&Ajax_CallBackMethod=LoadData&Ajax_RequestUrl=http%3A%2F%2Fwww.mtime.com%2Fmovie%2F"
							+ oriId + "%2Fplots.html&Ajax_CallBackArgument0=1&Ajax_CallBackArgument1=" + oriId;
				crawlURI.setCrawlUrl(crawlUrl);
				crawlURI.setCrawlStatus(CrawlStatusCodes.DEFAULT_STATUS);

				fetch.process(crawlURI);
				plotsPageProcessor.process(crawlURI);
				crawlURI.setCrawlStatus(CrawlStatusCodes.PAGE_PROCESS_SUCCESS);
				dbWriter.process(crawlURI);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e);
		}
	}
	
	
	public FetchHTTP getFetch() {
		return fetch;
	}

	public void setFetch(FetchHTTP fetch) {
		this.fetch = fetch;
	}

	public BaikeDataSourcePageProcessor getOverviewPageProcessor() {
		return overviewPageProcessor;
	}

	public void setOverviewPageProcessor(BaikeDataSourcePageProcessor overviewPageProcessor) {
		this.overviewPageProcessor = overviewPageProcessor;
	}

	public MoviePlotsPageProcessor getPlotsPageProcessor() {
		return plotsPageProcessor;
	}

	public void setPlotsPageProcessor(MoviePlotsPageProcessor plotsPageProcessor) {
		this.plotsPageProcessor = plotsPageProcessor;
	}

	public RatingInterfacePageProcessor getRatingPageProcessor() {
		return ratingPageProcessor;
	}

	public void setRatingPageProcessor(RatingInterfacePageProcessor ratingPageProcessor) {
		this.ratingPageProcessor = ratingPageProcessor;
	}

	public BaikeDataSourceDbWriter getDbWriter() {
		return dbWriter;
	}

	public void setDbWriter(BaikeDataSourceDbWriter dbWriter) {
		this.dbWriter = dbWriter;
	}

	public static void main(String[] args) {
		try {
			MtimeBaikeFastCrawlJob job = new MtimeBaikeFastCrawlJob();
			ApplicationContext acx = new ClassPathXmlApplicationContext("applicationContext.xml");
			UniversalBo universalBo = (UniversalBo) acx.getBean("universalBo");
			job.dbWriter.setUniversalBo(universalBo);

			InputStream in = ClassLoader.getSystemResourceAsStream("regex/www.mtime.com/overview.properties");
			Properties processPageRegex = new Properties();
			processPageRegex.load(in);
			BaikeDataSourcePageProcessor pageProcessor = new BaikeDataSourcePageProcessor(processPageRegex);
			job.overviewPageProcessor = pageProcessor;
			// 2009年6月10日结束ID：107542
			job.setStartVid(12407);
			job.setEndVid(120000);
			job.setMultiThreadFlag(true);
			job.doExecute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}